package com.okbihuo.perm.core.logger.event;


import com.okbihuo.perm.core.logger.domain.LogRecord;
import com.okbihuo.perm.core.logger.service.ILogHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 异步监听日志事件
 *
 * @author Chill
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class LogListener {

	private final ILogHandler logHandler;

	@Async
	@Order
	@EventListener(LogEvent.class)
	public void saveUsualLog(LogEvent event) {
		LogRecord logRecord = event.getLog();
		boolean save = logHandler.saveLog(logRecord);
		if(!save) {
			log.error("日志记录失败");
		}
	}

}
